//
//  SettingViewController.m
//  ShareYOU
//
//  Created by 杨亭 on 14-12-22.
//  Copyright (c) 2014年 ShareYOU. All rights reserved.
//

#import "SettingViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "EnterTheCategoryViewController.h"
#import "DataBaseHandler.h"

@interface SettingViewController ()<UIActionSheetDelegate>


@end

@implementation SettingViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //设置背景图片
//    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"add.jpg"]];
    
    self.view.backgroundColor = [UIColor whiteColor];

    CGFloat width = self.view.frame.size.width / 13;
    CGFloat height = self.view.frame.size.height / 9 ;
    //view显示
    
    //添加类型
    self.addTypeButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.addTypeButton.frame = CGRectMake(width * 3, height * 2, 144, 36);
    self.addTypeButton.tag = 1003;
    self.addTypeButton.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"add.png"]];
    [self.addTypeButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.addTypeButton setTitle:@"添加类型" forState:UIControlStateNormal];
    [self.addTypeButton addTarget:self action:@selector(addTypeAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.addTypeButton];
    
    //添加人员
    self.addPeopleButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.addPeopleButton.tag = 1004;
    self.addPeopleButton.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"add.png"]];
    [self.addPeopleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.addPeopleButton.frame = CGRectMake(width * 4, height * 3, 144, 36);
    [self.addPeopleButton setTitle:@"添加成员" forState:UIControlStateNormal];
    [self.addPeopleButton addTarget:self action:@selector(addPeopleAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.addPeopleButton];
    
    //删除添加按钮
    self.deleteAddButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.deleteAddButton.tag = 1005;
    self.deleteAddButton.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"add.png"]];
    [self.deleteAddButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

    self.deleteAddButton.frame = CGRectMake(width * 3, height * 4, 144, 36);
    [self.deleteAddButton setTitle:@"删除添加" forState:UIControlStateNormal];
    [self.deleteAddButton addTarget:self action:@selector(deleteAddAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.deleteAddButton];


    
    //删除按钮
    self.deleteButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.deleteButton.tag = 1001;
    self.deleteButton.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"add.png"]];
    [self.deleteButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

    self.deleteButton.frame = CGRectMake(width * 4, height * 5, 144, 36);
    [self.deleteButton setTitle:@"Delete All" forState:UIControlStateNormal];
    [self.deleteButton addTarget:self action:@selector(deleteData:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.deleteButton];

    
    //创建返回按钮
    self.navigationItem.hidesBackButton=YES;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回"  style:UIBarButtonItemStylePlain target:self action:@selector(backBtnAction)];//UIBarButtonItem

    //创建手势
    UISwipeGestureRecognizer *recognizer;
    
    recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];
    
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
    
    [[self view] addGestureRecognizer:recognizer];

    
    
}

//返回按钮方法的实现
-(void)backBtnAction{
    CATransition*transition=[CATransition animation];
    transition.duration=0.5;
    transition.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type=kCATransitionPush;
    transition.subtype=kCATransitionFromRight;//方向
    
    [self.navigationController.view setBackgroundColor:[UIColor whiteColor]];//
    [self.navigationController.view.layer addAnimation:transition forKey:nil];
    
    [self.navigationController popViewControllerAnimated:NO];
    [self.navigationController.view removeFromSuperview];
}
//手势方法的实现
-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer{
    
    if(recognizer.direction==UISwipeGestureRecognizerDirectionLeft) {
        CATransition*transition=[CATransition animation];
        transition.duration=0.3;
        transition.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        transition.type=kCATransitionPush;
        transition.subtype=kCATransitionFromRight;//方向
        
        [self.navigationController.view setBackgroundColor:[UIColor colorWithRed:0.0 green:156.0/255 blue:181.0/255 alpha:1]];
        [self.navigationController.view.layer addAnimation:transition forKey:nil];
        
        [self.navigationController popViewControllerAnimated:NO];
        
        //执行程序
        
    }
}


    
//添加成员按钮实现
- (void)addPeopleAction:(id)sender
{
    
    EnterTheCategoryViewController *enterVC = [[EnterTheCategoryViewController alloc] init];
    enterVC.str = @"addPeople";
    [self.navigationController pushViewController:enterVC animated:YES];
    [enterVC release];
    
    
}


#pragma mark 添加类型
- (void)addTypeAction:(id)sender
{
    EnterTheCategoryViewController *enterVC = [[EnterTheCategoryViewController alloc] init];
    enterVC.str = @"addType";
    [self.navigationController pushViewController:enterVC animated:YES];
    [enterVC release];
    
    
}


//删除所有按钮的实现
- (void)deleteData:(id)sender
{
    //弹出框
    UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"是否确认删除所有记账记录" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
    
    [alerView show];//让警告视图显示出来
    
    [alerView release];

    

}
//弹出框方法实现
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        
        
    }
    else if (buttonIndex == 1)
    {
        DataBaseHandler *dataHandler = [DataBaseHandler shareInstance];
        [dataHandler deleteWriteTable];

    }
    
}



//删除按钮的实现
- (void)deleteAddAction:(id)sender
{
    DataBaseHandler *dataHandler = [DataBaseHandler shareInstance];
    [dataHandler deleteTable];
    [dataHandler deletePersonnelTable];

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
